package com.mock.ems.controller.config;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.mock.ems.common.JsonResult;

/**
 * Created by MOCK on 2017/10/22.
 */
public class MallController extends Controller {

    public void index() {
        Record record = Db.findFirst("select * from banner_config where type = 10 ");
        if (null != record) {
            record.set("banners",record.getStr("banners").split(","));
            record.set("urls",record.getStr("urls").split(","));
            setAttr("mall",record.getColumns());
        }
        renderJsp("/page/zone/mall/mall_edit.jsp");
    }

    public void save() {
        //处理banners
        String banners = getPara("banners");
        String urls = getPara("bannerUrls");
        Record bannerConfig = Db.findFirst("select * from banner_config where type = 10 ");
        if (null == bannerConfig) {
            bannerConfig = new Record();
            bannerConfig.set("type",10)
                    .set("banners",banners)
                    .set("urls",urls);
            if (!Db.save("banner_config",bannerConfig)) {
                renderJson(new JsonResult().status("-1").show("保存失败"));
            }
        }else {
            bannerConfig.set("banners",banners)
                    .set("urls",urls);
            if (!Db.update("banner_config",bannerConfig)) {
                renderJson(new JsonResult().status("-1").show("保存失败"));
            }
        }
        renderJson(new JsonResult().status("000").show("保存成功"));
    }
}
